package com.dtz.rabbitmq.service;

import java.util.UUID;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dtz.rabbitmq.constant.RabbitConstant;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class TopicOrderService {

	@Autowired
	private RabbitTemplate rabbitTemplate;
	
	
	public void makeOrder(String userId, String productId, int num) {
		// 1： 模拟用户下单
        String orderNumer = UUID.randomUUID().toString();
        // 2: 根据商品id productId 去查询商品的库存
        // int numstore = productSerivce.getProductNum(productId);
        // 3:判断库存是否充足
        // if(num >  numstore ){ return  "商品库存不足..."; }
        // 4: 下单逻辑
        // orderService.saveOrder(order);
        // 5: 下单成功要扣减库存
        // 6: 下单完成以后
        log.info("用户 ：{},订单编号是：{}", userId, orderNumer);
        // 发送订单信息给RabbitMQ direct
        rabbitTemplate.convertAndSend(RabbitConstant.TOPIC_ORDER_EXCHANGE, "com.email.sms", orderNumer);
        log.info("发送消息到rabbitmq成功");
	}
	
}
